package cn.wolfcode.shop.cloud.mq;

import cn.wolfcode.shop.cloud.service.ISeckillGoodService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@RocketMQMessageListener(
        consumerGroup = MQConstans.CREATE_ORDER_FAIL_GROUP,
        topic = MQConstans.CREATE_ORDER,
        selectorExpression = MQConstans.CREATE_ORDER_FAIL_TAG
)
@Component
public class OrderFailMQListener implements RocketMQListener<Map> {
    @Autowired
    private ISeckillGoodService seckillGoodService;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Override
    public void onMessage(Map map) {
        System.out.println("消费下单失败的消息");
        //获取long类型的秒杀商品id
        Object seckillId = map.get("seckillId");
        Long goodId=null;
        if (seckillId instanceof Long){
            goodId= (Long) seckillId;
        }else if (seckillId instanceof Integer){
            goodId=((Integer) seckillId).longValue();
        }
        //下单失败恢复预先库存
        seckillGoodService.incrRedisStockCount(goodId);
        //发送消息修改本地标识
        rocketMQTemplate.syncSend(MQConstans.CREATE_ORDER_FLAG_DEST,goodId);

    }
}
